package com.itheima.reggie.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dot.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapeer.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealMapper setmealMapper;


    /*
    //新增套餐
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");

    }

    /*
    套餐分页查询
     */
    @GetMapping("/page")
    public R page(Integer page, Integer pageSize, String name) {
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> dtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(name != null, Setmeal::getName, name);
        qw.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo, qw);

        //套餐分类
        BeanUtil.copyProperties(pageInfo, dtoPage, "records");
        List<SetmealDto> collect = pageInfo.getRecords().stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtil.copyProperties(item, setmealDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);
            return setmealDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(collect);

        return R.success(dtoPage);
    }

    /*
    状态
     */
    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids) {
        LambdaUpdateWrapper<Setmeal> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Setmeal::getStatus, status);
        wrapper.in(Setmeal::getId, ids);
        setmealService.update(wrapper);
        return R.success("修改成功");
    }

    /*
    套餐删除
     */
    @DeleteMapping
    public R delete(Long[] ids) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids);
        wrapper.eq(Setmeal::getStatus, 1);
        int count = setmealMapper.selectCount(wrapper);
        if (count == 0) {
            for (int i = 0; i < ids.length; i++) {
                Long id = ids[i];
                LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
                qw.eq(Setmeal::getId, id);
                setmealService.remove(qw);
            }
        }
        return count == 0 ? R.success("删除成功") : R.error("删除失败");
    }

    /*
    根据id查询套餐菜品数据
     */
    @GetMapping("/{id}")
    public R list(@PathVariable Long id){
        SetmealDto byIdWithDishes = setmealService.getByIdWithDishes(id);

        return R.success(byIdWithDishes);
    }

    /*
    套餐修改
     */
    @PutMapping
    public R update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDishes(setmealDto);
        return R.success("套餐修改成功");
    }

    @GetMapping("/list")
    public R setMeal(Long categoryId,Integer status){
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(categoryId!=null,Setmeal::getCategoryId,categoryId);
        qw.eq(status!=null,Setmeal::getStatus,status);
        List<Setmeal> list = setmealService.list(qw);
        return R.success(list);
    }



}
